import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import PetView from '@/views/PetInfo/PetView.vue'
import DetailView from '@/views/PetInfo/DetailView.vue'
import PetCare from '@/views/PetInfo/PetCare.vue'
import NoticeView from '@/components/NoticeView.vue'
import Login from '@/views/login/index.vue'
import User from '@/views/user/index.vue'
import Search from '@/views/search/index.vue'
import Pay from '@/views/pay/index.vue'
import UserInfo from '@/views/user/components/UserInfo.vue'
import UserAddress from '@/views/user/components/UserAddress.vue'
import UserOrder from '@/views/user/components/UserOrder.vue'
import AdminLogin from '@/views/admin/Login.vue'
import AdminHome from '@/views/admin/index.vue'
import UserManagement from '@/views/admin/UserManagement.vue'
import PetManagement from '@/views/admin/PetManagement.vue'
import OrderManagement from '@/views/admin/OrderManagement.vue'
import NoticeManagement from '@/views/admin/NoticeManagement.vue'
import PetGroomingManagement from '@/views/admin/PetGroomingManagement.vue'
import NotFound from '@/views/NotFound/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/admin',
    name: 'admin',
    component: AdminLogin,
  },
  {
    path: '/admin/home',
    name: 'adminHome',
    redirect: '/admin/home/userManagement',
    component: AdminHome,
    meta: {
      isAuth: true,
    },
    children: [
      {
        path: 'userManagement',
        name: 'userManagement',
        component: UserManagement,
        meta: {
          isAuth: true,
        },
      },
      {
        path: 'petManagement',
        name: 'petManagement',
        component: PetManagement,
        meta: {
          isAuth: true,
        },
      },
      {
        path: 'orderManagement',
        name: 'orderManagement',
        component: OrderManagement,
        meta: {
          isAuth: true,
        },
      },
      {
        path: 'noticeManagement',
        name: 'noticeManagement',
        component: NoticeManagement,
        meta: {
          isAuth: true,
        },
      },
      {
        path: 'petGroomingManagement',
        name: 'petGroomingManagement',
        component: PetGroomingManagement,
        meta: {
          isAuth: true,
        },
      },
    ],
  },
  {
    path: '/',
    name: 'home',
    component: HomeView,
  },
  {
    path: '/pet',
    name: 'pet',
    component: PetView,
    children: [
      {
        path: 'info/:No',
        name: 'info',
        component: DetailView,
      },
      {
        path: 'care',
        name: 'care',
        component: PetCare,
      },
    ],
  },
  {
    path: '/notice',
    name: 'notice',
    component: NoticeView,
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
  {
    path: '/user',
    redirect: '/user/center',
    name: 'user',
    component: User,
    // 鉴权访问 isAuth:true 则需要判断权限
    meta: {
      isAuth: true,
    },
    children: [
      {
        path: 'center',
        name: 'center',
        component: UserInfo,
        meta: {
          isAuth: true,
        },
      },
      {
        path: 'address',
        name: 'address',
        component: UserAddress,
        meta: {
          isAuth: true,
        },
      },
      {
        path: 'order',
        name: 'order',
        component: UserOrder,
        meta: {
          isAuth: true,
        },
      },
    ],
  },
  {
    path: '/search',
    name: 'search',
    component: Search,
  },
  {
    path: '/pay/:No',
    name: 'pay',
    component: Pay,
    meta: {
      isAuth: true,
    },
  },
  {
    path: '/:any(.*)',
    component: NotFound
  }
]

const router = new VueRouter({
  mode: 'history',
  routes,
})

// 解决重复点击this.$router.push/replace报错问题
const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject)
  } else {
    originPush.call(
      this,
      location,
      () => {},
      () => {},
    )
  }
}

VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject)
  } else {
    originReplace.call(
      this,
      location,
      () => {},
      () => {},
    )
  }
}

// 全局前置守卫：初始化时执行、每次路由切换前执行
router.beforeEach((to, from, next) => {
  // 判断当前路由是否需要进行权限控制
  if (to.meta.isAuth) {
    const tokenStr = window.sessionStorage.getItem('token')
    if (tokenStr) {
      //权限控制的具体规则
      next()
    } else {
      alert('请登陆后操作！')
      next('/')
    }
    // 访问非鉴权页面直接放行
  } else {
    next()
  }
})

export default router
